//
//  ViewController.m
//  app
//
//  Created by KL on 16/12/27.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
#import "View2Controller.h"
@interface ViewController ()
{
    NSMutableArray<Person *>*phone;
    UISearchController *sear;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"联系人";
    phone = [NSMutableArray new];
    Person *p = [Person new];
    [p initWithName:@"dcjd" withTel:@"111111"];
    [phone addObject:p];
    
    Person *p1 = [Person new];
    [p1 initWithName:@"scdzsli" withTel:@"455222222"];
    [phone addObject:p1];
    
    Person *p2 = [Person new];
    [p2 initWithName:@"cjbjkc" withTel:@"5454666966"];
    [phone addObject:p2];
    
    Person *p3 = [Person new];
    [p3 initWithName:@"sck" withTel:@"4465435413"];
    [phone addObject:p3];
    
    Person *p4 = [Person new];
    [p4 initWithName:@"cvcxv" withTel:@"4465435413"];
    [phone addObject:p4];
    
    Person *p5 = [Person new];
    [p5 initWithName:@"xcsdd" withTel:@"4465435413"];
    [phone addObject:p5];
    
    Person *p6 = [Person new];
    [p6 initWithName:@"zsds" withTel:@"4465435413"];
    [phone addObject:p6];

    Person *p7 = [Person new];
    [p7 initWithName:@"qads" withTel:@"4465435413"];
    [phone addObject:p7];
    
    Person *p8 = [Person new];
    [p8 initWithName:@"sdasxs" withTel:@"4465435413"];
    [phone addObject:p8];
    
    Person *p9 = [Person new];
    [p9 initWithName:@"wdedef" withTel:@"4465435413"];
    [phone addObject:p9];


    View2Controller *con = [View2Controller new];
    sear = [[UISearchController alloc]initWithSearchResultsController:con];
    self.definesPresentationContext = YES;
    self.tableView.tableHeaderView = sear.searchBar;
    con.text = sear.searchBar.text;
    [con.data arrayByAddingObject:phone];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return phone.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Person *po = phone[indexPath.row];
    cell.textLabel.text = po.name;
    cell.detailTextLabel.text = po.tel;
    return cell;
}


- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *row = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSInteger index = indexPath.row;
        [phone removeObjectAtIndex:index];//removeAtIndex(indexPath)
        [tableView reloadData];
    }];
    UITableViewRowAction *row1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"排序" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    }];

    return @[row,row1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
